package com.project.decoration.controller.time;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.decoration.common.enums.BusinessType;
import com.project.decoration.common.enums.ResultCode;
import com.project.decoration.domain.DecorationTime;
import com.project.decoration.domain.Result;
import com.project.decoration.service.DecorationTimeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 超级管理员
 * @version 1.0
 * @description: 项目时间线controller
 * @date 2025/02/17 09:45
 */
@Controller
@ResponseBody
@RequestMapping("time")
public class DecorationTimeController {

    @Autowired
    private DecorationTimeService decorationTimeService;

    /** 分页获取项目时间线 */
    @PostMapping("getDecorationTimePage")
    public Result getDecorationTimePage(@RequestBody DecorationTime decorationTime) {
        Page<DecorationTime> page = new Page<>(decorationTime.getPageNumber(),decorationTime.getPageSize());
        QueryWrapper<DecorationTime> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(StringUtils.isNotBlank(decorationTime.getProjectId()),DecorationTime::getProjectId,decorationTime.getProjectId())
                .eq(decorationTime.getStartTime() != null,DecorationTime::getStartTime,decorationTime.getStartTime())
                .eq(decorationTime.getEndTime() != null,DecorationTime::getEndTime,decorationTime.getEndTime())
                .eq(decorationTime.getType() != null,DecorationTime::getType,decorationTime.getType());
        Page<DecorationTime> decorationTimePage = decorationTimeService.page(page, queryWrapper);
        return Result.success(decorationTimePage);
    }

    @GetMapping("getDecorationTimeByProjectId")
    public Result getDecorationTimeByProjectId(@RequestParam("projectId") String projectId) {
        QueryWrapper<DecorationTime> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(DecorationTime::getProjectId,projectId);
        List<DecorationTime> timeList = decorationTimeService.list(queryWrapper);
        return Result.success(timeList);
    }

    /** 根据id获取项目时间线 */
    @GetMapping("getDecorationTimeById")
    public Result getDecorationTimeById(@RequestParam("id")String id) {
        DecorationTime decorationTime = decorationTimeService.getById(id);
        return Result.success(decorationTime);
    }

    /** 保存项目时间线 */
    @PostMapping("saveDecorationTime")
    public Result saveDecorationTime(@RequestBody DecorationTime decorationTime) {
        boolean save = decorationTimeService.save(decorationTime);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 编辑项目时间线 */
    @PostMapping("editDecorationTime")
    public Result editDecorationTime(@RequestBody DecorationTime decorationTime) {
        boolean save = decorationTimeService.updateById(decorationTime);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 删除项目时间线 */
    @GetMapping("removeDecorationTime")
    public Result removeDecorationTime(@RequestParam("ids")String ids) {
        if (StringUtils.isNotBlank(ids)) {
            String[] asList = ids.split(",");
            for (String id : asList) {
                decorationTimeService.removeById(id);
            }
            return Result.success();
        } else {
            return Result.fail("项目时间线id不能为空！");
        }
    }

}
